home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / gould-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-10  |  14.0 KB  |  561 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25.  
  26. #include <sys/param.h>
  27. #include <sys/dir.h>
  28. #include <signal.h>
  29. #include <sys/user.h>
  30. #include <sys/ioctl.h>
  31. #include <fcntl.h>
  32.  
  33. #include <a.out.h>
  34. #include <sys/file.h>
  35. #include <sys/stat.h>
  36.  
  37. extern int errno;
  38.  
  39. /* This function simply calls ptrace with the given arguments.  
  40.    It exists so that all calls to ptrace are isolated in this 
  41.    machine-dependent file. */
  42. int
  43. call_ptrace (request, pid, arg3, arg4)
  44.      int request, pid, arg3, arg4;
  45. {
  46.   return ptrace (request, pid, arg3, arg4);
  47. }
  48.  
  49. kill_inferior ()
  50. {
  51.   if (remote_debugging)
  52.     return;
  53.   if (inferior_pid == 0)
  54.     return;
  55.   ptrace (8, inferior_pid, 0, 0);
  56.   wait (0);
  57.   inferior_died ();
  58. }
  59.  
  60. /* This is used when GDB is exiting.  It gives less chance of error.*/
  61.  
  62. kill_inferior_fast ()
  63. {
  64.   if (remote_debugging)
  65.     return;
  66.   if (inferior_pid == 0)
  67.     return;
  68.   ptrace (8, inferior_pid, 0, 0);
  69.   wait (0);
  70. }
  71.  
  72. /* Resume execution of the inferior process.
  73.    If STEP is nonzero, single-step it.
  74.    If SIGNAL is nonzero, give it that signal.  */
  75.  
  76. void
  77. resume (step, signal)
  78.      int step;
  79.      int signal;
  80. {
  81.   errno = 0;
  82.   if (remote_debugging)
  83.     remote_resume (step, signal);
  84.   else
  85.     {
  86.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  87.       if (errno)
  88.     perror_with_name ("ptrace");
  89.     }
  90. }
  91.  
  92. void
  93. fetch_inferior_registers ()
  94. {
  95.   register int regno;
  96.   register unsigned int regaddr;
  97.   char buf[MAX_REGISTER_RAW_SIZE];
  98.   register int i;
  99.  
  100.   struct user u;
  101.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  102.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  103.  
  104.   for (regno = 0; regno < NUM_REGS; regno++)
  105.     {
  106.       regaddr = register_addr (regno, offset);
  107.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  108.      {
  109.        *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  110.        regaddr += sizeof (int);
  111.      }
  112.       supply_register (regno, buf);
  113.     }
  114. }
  115.  
  116. /* Store our register values back into the inferior.
  117.    If REGNO is -1, do this for all registers.
  118.    Otherwise, REGNO specifies which register (so we can save time).  */
  119.  
  120. store_inferior_registers (regno)
  121.      int regno;
  122. {
  123.   register unsigned int regaddr;
  124.   char buf[80];
  125.  
  126.   struct user u;
  127.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  128.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  129.  
  130.   if (regno >= 0)
  131.     {
  132.       regaddr = register_addr (regno, offset);
  133.       errno = 0;
  134.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  135.       if (errno != 0)
  136.     {
  137.       sprintf (buf, "writing register number %d", regno);
  138.       perror_with_name (buf);
  139.     }
  140.     }
  141.   else for (regno = 0; regno < NUM_REGS; regno++)
  142.     {
  143.       regaddr = register_addr (regno, offset);
  144.       errno = 0;
  145.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  146.       if (errno != 0)
  147.     {
  148.       sprintf (buf, "writing register number %d", regno);
  149.       perror_with_name (buf);
  150.     }
  151.     }
  152. }
  153.  
  154. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  155.    in the NEW_SUN_PTRACE case.
  156.    It ought to be straightforward.  But it appears that writing did
  157.    not write the data that I specified.  I cannot understand where
  158.    it got the data that it actually did write.  */
  159.  
  160. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  161.    to debugger memory starting at MYADDR. 
  162.    On failure (cannot read from inferior, usually because address is out
  163.    of bounds) returns the value of errno. */
  164.  
  165. int
  166. read_inferior_memory (memaddr, myaddr, len)
  167.      CORE_ADDR memaddr;
  168.      char *myaddr;
  169.      int len;
  170. {
  171.   register int i;
  172.   /* Round starting address down to longword boundary.  */
  173.   register CORE_ADDR addr = memaddr & - sizeof (int);
  174.   /* Round ending address up; get number of longwords that makes.  */
  175.   register int count
  176.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  177.   /* Allocate buffer of that many longwords.  */
  178.   register int *buffer = (int *) alloca (count * sizeof (int));
  179.   extern int errno;
  180.  
  181.   /* Read all the longwords */
  182.   for (i = 0; i < count; i++, addr += sizeof (int))
  183.     {
  184.       errno = 0;
  185.       if (remote_debugging)
  186.     buffer[i] = remote_fetch_word (addr);
  187.       else
  188.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  189.       if (errno)
  190.     return errno;
  191.     }
  192.  
  193.   /* Copy appropriate bytes out of the buffer.  */
  194.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  195.   return 0;
  196. }
  197.  
  198. /* Copy LEN bytes of data from debugger memory at MYADDR
  199.    to inferior's memory at MEMADDR.
  200.    On failure (cannot write the inferior)
  201.    returns the value of errno.  */
  202.  
  203. int
  204. write_inferior_memory (memaddr, myaddr, len)
  205.      CORE_ADDR memaddr;
  206.      char *myaddr;
  207.      int len;
  208. {
  209.   register int i;
  210.   /* Round starting address down to longword boundary.  */
  211.   register CORE_ADDR addr = memaddr & - sizeof (int);
  212.   /* Round ending address up; get number of longwords that makes.  */
  213.   register int count
  214.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  215.   /* Allocate buffer of that many longwords.  */
  216.   register int *buffer = (int *) alloca (count * sizeof (int));
  217.   extern int errno;
  218.  
  219.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  220.  
  221.   if (remote_debugging)
  222.     buffer[0] = remote_fetch_word (addr);
  223.   else
  224.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  225.  
  226.   if (count > 1)
  227.     {
  228.       if (remote_debugging)
  229.     buffer[count - 1]
  230.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  231.       else
  232.     buffer[count - 1]
  233.       = ptrace (1, inferior_pid,
  234.             addr + (count - 1) * sizeof (int), 0);
  235.     }
  236.  
  237.   /* Copy data to be written over corresponding part of buffer */
  238.  
  239.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  240.  
  241.   /* Write the entire buffer.  */
  242.  
  243.   for (i = 0; i < count; i++, addr += sizeof (int))
  244.     {
  245.       errno = 0;
  246.       if (remote_debugging)
  247.     remote_store_word (addr, buffer[i]);
  248.       else
  249.     ptrace (4, inferior_pid, addr, buffer[i]);
  250.       if (errno)
  251.     return errno;
  252.     }
  253.  
  254.   return 0;
  255. }
  256.  
  257. /* Work with core dump and executable files, for GDB. 
  258.    This code would be in core.c if it weren't machine-dependent. */
  259.  
  260. /* This should probably be deleted.  */
  261. /* Recognize COFF format systems because a.out.h defines AOUTHDR.  */
  262. #ifdef AOUTHDR
  263. #define COFF_FORMAT
  264. #endif
  265.  
  266. #ifndef N_TXTADDR
  267. #define N_TXTADDR(hdr) 0
  268. #endif /* no N_TXTADDR */
  269.  
  270. #ifndef N_DATADDR
  271. #define N_DATADDR(hdr) hdr.a_text
  272. #endif /* no N_DATADDR */
  273.  
  274. /* Make COFF and non-COFF names for things a little more compatible
  275.    to reduce conditionals later.  */
  276.  
  277. #ifdef COFF_FORMAT
  278. #define a_magic magic
  279. #endif
  280.  
  281. #ifndef COFF_FORMAT
  282. #ifndef AOUTHDR
  283. #define AOUTHDR struct exec
  284. #endif
  285. #endif
  286.  
  287. extern char *sys_siglist[];
  288.  
  289.  
  290. /* Hook for `exec_file_command' command to call.  */
  291.  
  292. extern void (*exec_file_display_hook) ();
  293.    
  294. /* File names of core file and executable file.  */
  295.  
  296. extern char *corefile;
  297. extern char *execfile;
  298.  
  299. /* Descriptors on which core file and executable file are open.
  300.    Note that the execchan is closed when an inferior is created
  301.    and reopened if the inferior dies or is killed.  */
  302.  
  303. extern int corechan;
  304. extern int execchan;
  305.  
  306. /* Last modification time of executable file.
  307.    Also used in source.c to compare against mtime of a source file.  */
  308.  
  309. extern int exec_mtime;
  310.  
  311. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  312.  
  313. extern CORE_ADDR data_start;
  314. extern CORE_ADDR data_end;
  315. extern CORE_ADDR stack_start;
  316. extern CORE_ADDR stack_end;
  317.  
  318. /* Virtual addresses of bounds of two areas of memory in the exec file.
  319.    Note that the data area in the exec file is used only when there is no core file.  */
  320.  
  321. extern CORE_ADDR text_start;
  322. extern CORE_ADDR text_end;
  323.  
  324. extern CORE_ADDR exec_data_start;
  325. extern CORE_ADDR exec_data_end;
  326.  
  327. /* Address in executable file of start of text area data.  */
  328.  
  329. extern int text_offset;
  330.  
  331. /* Address in executable file of start of data area data.  */
  332.  
  333. extern int exec_data_offset;
  334.  
  335. /* Address in core file of start of data area data.  */
  336.  
  337. extern int data_offset;
  338.  
  339. /* Address in core file of start of stack area data.  */
  340.  
  341. extern int stack_offset;
  342.  
  343. #ifdef COFF_FORMAT
  344. /* various coff data structures */
  345.  
  346. extern FILHDR file_hdr;
  347. extern SCNHDR text_hdr;
  348. extern SCNHDR data_hdr;
  349.  
  350. #endif /* not COFF_FORMAT */
  351.  
  352. /* a.out header saved in core file.  */
  353.   
  354. extern AOUTHDR core_aouthdr;
  355.  
  356. /* a.out header of exec file.  */
  357.  
  358. extern AOUTHDR exec_aouthdr;
  359.  
  360. extern void validate_files ();
  361.  
  362. core_file_command (filename, from_tty)
  363.      char *filename;
  364.      int from_tty;
  365. {
  366.   int val;
  367.   extern char registers[];
  368.  
  369.   /* Discard all vestiges of any previous core file
  370.      and mark data and stack spaces as empty.  */
  371.  
  372.   if (corefile)
  373.     free (corefile);
  374.   corefile = 0;
  375.  
  376.   if (corechan >= 0)
  377.     close (corechan);
  378.   corechan = -1;
  379.  
  380.   data_start = 0;
  381.   data_end = 0;
  382.   stack_start = STACK_END_ADDR;
  383.   stack_end = STACK_END_ADDR;
  384.  
  385.   /* Now, if a new core file was specified, open it and digest it.  */
  386.  
  387.   if (filename)
  388.     {
  389.       filename = tilde_expand (filename);
  390.       make_cleanup (free, filename);
  391.       
  392.       if (have_inferior_p ())
  393.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  394.       corechan = open (filename, O_RDONLY, 0);
  395.       if (corechan < 0)
  396.     perror_with_name (filename);
  397.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  398.       {
  399.     struct user u;
  400.     int reg_offset;
  401.  
  402.     val = myread (corechan, &u, sizeof u);
  403.     if (val < 0)
  404.       perror_with_name (filename);
  405.     data_start = exec_data_start;
  406.  
  407.     data_end = data_start + NBPG * u.u_dsize;
  408.     stack_start = stack_end - NBPG * u.u_ssize;
  409.     data_offset = NBPG * UPAGES;
  410.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  411.     reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  412.  
  413.     /* I don't know where to find this info.
  414.        So, for now, mark it as not available.  */
  415.     core_aouthdr.a_magic = 0;
  416.  
  417.     /* Read the register values out of the core file and store
  418.        them where `read_register' will find them.  */
  419.  
  420.     {
  421.       register int regno;
  422.  
  423.       for (regno = 0; regno < NUM_REGS; regno++)
  424.         {
  425.           char buf[MAX_REGISTER_RAW_SIZE];
  426.  
  427.           val = lseek (corechan, register_addr (regno, reg_offset), 0);
  428.           if (val < 0)
  429.         perror_with_name (filename);
  430.  
  431.            val = myread (corechan, buf, sizeof buf);
  432.           if (val < 0)
  433.         perror_with_name (filename);
  434.           supply_register (regno, buf);
  435.         }
  436.     }
  437.       }
  438.       if (filename[0] == '/')
  439.     corefile = savestring (filename, strlen (filename));
  440.       else
  441.     {
  442.       corefile = concat (current_directory, "/", filename);
  443.     }
  444.  
  445.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  446.                         read_pc ()));
  447.       select_frame (get_current_frame (), 0);
  448.       validate_files ();
  449.     }
  450.   else if (from_tty)
  451.     printf ("No core file now.\n");
  452. }
  453.  
  454. exec_file_command (filename, from_tty)
  455.      char *filename;
  456.      int from_tty;
  457. {
  458.   int val;
  459.  
  460.   /* Eliminate all traces of old exec file.
  461.      Mark text segment as empty.  */
  462.  
  463.   if (execfile)
  464.     free (execfile);
  465.   execfile = 0;
  466.   data_start = 0;
  467.   data_end -= exec_data_start;
  468.   text_start = 0;
  469.   text_end = 0;
  470.   exec_data_start = 0;
  471.   exec_data_end = 0;
  472.   if (execchan >= 0)
  473.     close (execchan);
  474.   execchan = -1;
  475.  
  476.   /* Now open and digest the file the user requested, if any.  */
  477.  
  478.   if (filename)
  479.     {
  480.       filename = tilde_expand (filename);
  481.       make_cleanup (free, filename);
  482.       
  483.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  484.             &execfile);
  485.       if (execchan < 0)
  486.     perror_with_name (filename);
  487.  
  488. #ifdef COFF_FORMAT
  489.       {
  490.     int aout_hdrsize;
  491.     int num_sections;
  492.  
  493.     if (read_file_hdr (execchan, &file_hdr) < 0)
  494.       error ("\"%s\": not in executable format.", execfile);
  495.  
  496.     aout_hdrsize = file_hdr.f_opthdr;
  497.     num_sections = file_hdr.f_nscns;
  498.  
  499.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  500.       error ("\"%s\": can't read optional aouthdr", execfile);
  501.  
  502.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  503.                   aout_hdrsize) < 0)
  504.       error ("\"%s\": can't read text section header", execfile);
  505.  
  506.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  507.                   aout_hdrsize) < 0)
  508.       error ("\"%s\": can't read data section header", execfile);
  509.  
  510.     text_start = exec_aouthdr.text_start;
  511.     text_end = text_start + exec_aouthdr.tsize;
  512.     text_offset = text_hdr.s_scnptr;
  513.     exec_data_start = exec_aouthdr.data_start;
  514.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  515.     exec_data_offset = data_hdr.s_scnptr;
  516.     data_start = exec_data_start;
  517.     data_end += exec_data_start;
  518.     exec_mtime = file_hdr.f_timdat;
  519.       }
  520. #else /* not COFF_FORMAT */
  521.       {
  522.     struct stat st_exec;
  523.  
  524.     FILHDR exec_coffhdr;
  525.  
  526.         val = myread (execchan, &exec_coffhdr, sizeof exec_coffhdr);
  527.         if (val < 0)
  528.           perror_with_name (filename);
  529.  
  530.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  531.  
  532.     if (val < 0)
  533.       perror_with_name (filename);
  534.  
  535.         text_start = N_TXTADDR (exec_aouthdr);
  536.         exec_data_start = N_DATADDR (exec_aouthdr);
  537.  
  538.         text_offset = N_TXTOFF (exec_coffhdr, exec_aouthdr);
  539.         exec_data_offset = N_TXTOFF (exec_coffhdr, exec_aouthdr)
  540.                 + exec_aouthdr.a_text;
  541.  
  542.     text_end = text_start + exec_aouthdr.a_text;
  543.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  544.     data_start = exec_data_start;
  545.     data_end += exec_data_start;
  546.  
  547.     fstat (execchan, &st_exec);
  548.     exec_mtime = st_exec.st_mtime;
  549.       }
  550. #endif /* not COFF_FORMAT */
  551.  
  552.       validate_files ();
  553.     }
  554.   else if (from_tty)
  555.     printf ("No exec file now.\n");
  556.  
  557.   /* Tell display code (if any) about the changed file name.  */
  558.   if (exec_file_display_hook)
  559.     (*exec_file_display_hook) (filename);
  560. }
  561.